<template>
  <transition name="list-fade">
    <div class="playlist" v-show="playListshow" @click="hide">
      <div class="list-wrapper" @click.stop>
        <div class="list-header">
          <h1 class="title">
            <i class="icon iconfont" :class="iconMode" @click="changeMode(hastips=false)"></i>
            <span class="text">{{PlayModeTips}}</span>
            <trash @iconClicked="deleteAll" ref="trash" expandClick expandValue></trash>
          </h1>
        </div>
        <scroll class="list-content" :data="sequenceList" ref="listcontent" :refreshDelay="100">
          <transition-group name="list" tag="ul">
            <li
              class="item"
              v-for="(item,index) in sequenceList"
              :key="item.id"
              @click="selectItem(item,index)"
              ref="listItem"
            >
              <i class="current" :class="getCurrentIcon(item)"></i>
              <span class="text">{{item.name}}</span>
              <span class="like" @click.stop="toggleFavorite(item)">
                <i class="iconfont" :class="getFavoriteIcon(item)"></i>
              </span>
              <span class="delete" @click.stop="deleteItem(item)">
                <i class="iconfont iconchuyidong icon-delete"></i>
              </span>
            </li>
          </transition-group>
        </scroll>
        <div class="list-operate">
          <div class="add" @click="addSong">
            <i class="icon-add iconfont iconjia1"></i>
            <span class="text">添加歌曲到队列</span>
          </div>
        </div>
        <div class="list-close" @click="hide">
          <span>关闭</span>
        </div>
      </div>
      <add-song ref="addSong"></add-song>
    </div>
  </transition>
</template>

<script>
import { mapGetters, mapMutations, mapActions } from "vuex";
import { playMode } from "@/utils/config";
import { Dialog } from "vant";
import { playerMixin } from "@/common/mixin";
import Scroll from "@/base/scroll/scroll";
import Trash from "@/base/icons/trash";
import AddSong from "@/components/add-song/add-song";

export default {
  mixins: [playerMixin],
  data() {
    return {
      playListshow: false
    };
  },
  computed: {},
  methods: {
    show() {
      this.playListshow = true;
      setTimeout(() => {
        this.$refs.listcontent.refresh();
        this.scrollToCurrent(this.currentSong);
      }, 20);
    },
    hide() {
      this.playListshow = false;
    },
    getCurrentIcon(item) {
      // 如果当前播放的歌曲id是列表项的id则给他加上图标：
      if (this.currentSong.id === item.id) {
        return "iconfont iconerji";
      } else {
        return "";
      }
    },
    selectItem(item, index) {
      // 如果播放模式是随机,则需要找到他在列表中的索引
      if (this.mode === playMode.random) {
        index = this.playList.findIndex(song => {
          return song.id === item.id;
        });
      }
      //  设置当前播放索引，等于切歌
      this.setCurrentIndex(index);
      //   设置播放状态为播放
      this.setPlayingState(true);
    },
    scrollToCurrent(current) {
      const index = this.sequenceList.findIndex(song => {
        return current.id === song.id;
      });
      this.$refs.listcontent.scrollToElement(this.$refs.listItem[index], 400);
    },
    ...mapActions(["deleteSong", "deleteSongList"]),
    // 删除歌曲：
    deleteItem(item) {
      this.deleteSong(item);
      if (!this.playList.length) {
        this.hide();
      }
    },
    // 删除全部歌曲：
    deleteAll() {
      Dialog.confirm({
        message: "清空播放列表?",
        className: "dialog_themedark",
        confirmButtonText: "清空",
        width: "250px"
      })
        .then(() => {
          this.deleteSongList();
          this.$refs.trash.close();
        })
        .catch(() => {
          this.$refs.trash.close();
        });
    },
    // 添加歌曲：
    addSong() {
      this.$refs.addSong.show();
    }
  },
  watch: {
    currentSong(newSong, oldSong) {
      if (!this.playListshow || newSong.id === oldSong.id) {
        return;
      }
      this.scrollToCurrent(newSong);
    }
  },
  components: {
    Scroll,
    [Dialog.Component.name]: Dialog.Component,
    Trash,
    AddSong
  }
};
</script>

<style lang="less" scoped>
.playlist {
  position: fixed;
  left: 0;
  right: 0;
  top: 0;
  bottom: 0;
  z-index: 999;
  background-color: @color-background-d;
  &.list-fade-enter-active,
  &.list-fade-leave-active {
    transition: opacity 0.3s;
    .list-wrapper {
      transition: all 0.3s;
    }
  }
  &.list-fade-enter,
  &.list-fade-leave-to {
    opacity: 0;
    .list-wrapper {
      transform: translate3d(0, 100%, 0);
    }
  }
  .list-wrapper {
    position: absolute;
    left: 0;
    bottom: 0;
    width: 100%;
    background-color: @color-highlight-background;
    border-top-left-radius: 20px;
    border-top-right-radius: 20px;
    overflow: hidden;
    .list-header {
      position: relative;
      padding: 20px 30px 10px 20px;

      overflow: hidden;
      .title {
        display: -webkit-box;
        display: -ms-flexbox;
        display: flex;
        -webkit-box-align: center;
        -ms-flex-align: center;
        align-items: center;
        .icon {
          margin-right: 10px;
          font-size: 30px;
          &.iconlike {
            color: @color-sub-theme;
          }
        }
        .text {
          flex: 1;
          font-size: @font-size-medium;
          color: #fff;
        }
        .clear {
          .extend-click;
          position: relative;
          font-size: @font-size-medium;
          color: #fff;
          .iconlajitong {
          }
        }
      }
    }
    .list-move {
      transition: transform 1s;
    }
    .list-content {
      max-height: 240px;
      overflow: hidden;
      .item {
        display: flex;
        align-items: center;
        height: 40px;
        padding: 0 30px 0 20px;
        overflow: hidden;
        // 删除的时候高度是40变为0的效果
        &.list-enter-active,
        &.list-leave-active {
          transition: all 0.1s;
        }
        &.list-enter,
        &.list-leave-to {
          height: 0;
        }
        .current {
          flex: 0 0 20px;
          width: 20px;
          font-size: @font-size-small;
          color: @mc;
        }
        .text {
          flex: 1;
          .sl;
          font-size: @font-size-medium;
          color: #f9f9f9;
        }
        .like {
          .extend-click();
          margin-right: 15px;
          font-size: @font-size-small;
          color: @color-theme;
          .iconlike {
            color: @color-sub-theme;
          }
        }
        .delete {
          .extend-click();
          font-size: @font-size-small;
          color: @color-theme;
        }
      }
    }
    .list-operate {
      width: 140px;
      margin: 20px auto 30px auto;
      .add {
        display: flex;
        align-items: center;
        padding: 8px 16px;
        border: 1px solid #fff;
        border-radius: 100px;
        color: #fff;
        .icon-add {
          margin-right: 5px;
          font-size: @font-size-small-s;
        }
        .text {
          font-size: @font-size-small;
        }
      }
    }
    .list-close {
      text-align: center;
      line-height: 50px;
      background: @color-background;
      font-size: @font-size-medium-x;
      color: @color-text-l;
    }
  }
}
</style>